﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RamGecTools;
using System.Drawing;

namespace Settings4Net_Test
{
    [TestClass()]
    public class ScenarioTest
    {
        [TestMethod()]
        public void TestScenario1()
        {
            Settings4Net target = new Settings4Net();
            target.Settings["booltest"] = true;
            target.Settings["fonttest"] = new Font("Arial", 5, FontStyle.Strikeout);
            target.Settings["datetest"] = new DateTime(1985, 8, 20);

            target.SetItemCategory("booltest", "cat");
            target.SetItemDescription("booltest", "desc");
            target.SetItemTag("booltest", "tag");
            target.SetItemVisibility("booltest", true);

            target.SetItemVisibility("fonttest", false);
            target.SetDefaultItemValue("fonttest", new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Pixel, 0));
            target.SetReadonlyProperty("fonttest", true);

            target.Add("doubletest", 1.123, "desc2", "cat2", "tag2", true);
            target.Settings["doubletest2"] = target.Settings["doubletest"];
            target.SetDefaultItemValue("doubletest2", 3.14);

            target.Add<Color>("colortest", Color.PowderBlue, "powder blue");
            target.Add<string>("stringtest", "test", "desc3", "cat3", "tag3");

            target.Settings["inttest"] = 12;
            target.SetMinItemValue("inttest", 8);
            target.SetMaxItemValue("inttest", 20);
            target.SetReadonlyProperty("inttest", true);

            target.Save("test_scenario1.xml");

            Assert.AreEqual(false, target.Open("nonexistant"));
            Assert.AreEqual(false, target.Open("none", false, true));

            Assert.AreEqual(true, target.Open("test_scenario1.xml"));

            Assert.AreEqual(true, target.Settings["booltest"]);
            Assert.AreEqual(true, ((Font)target.Settings["fonttest"]).Strikeout);
            Assert.AreEqual(1985, ((DateTime)target.Settings["datetest"]).Year);
            Assert.AreEqual(1.123, target.Settings["doubletest"]);
            Assert.AreEqual(Color.PowderBlue, target.Settings["colortest"]);
            Assert.AreEqual("test", target.Settings["stringtest"]);
            Assert.AreEqual("desc2", target.GetItemDescription("doubletest"));
            Assert.AreEqual(7, target.GetItemOrDefaultValue("none", 7));
            Assert.AreEqual(12, target.GetItemOrDefaultValue("inttest", 7));
            target.Settings["inttest"] = 9999;
            Assert.AreEqual(12, target.Settings["inttest"]);
            target.Settings["inttest"] = 0;
            Assert.AreEqual(12, target.Settings["inttest"]);
            Assert.AreEqual("Tahoma", ((Font)(target.GetDefaultItemValue("fonttest"))).FontFamily.Name);
            target.SetReadonlyProperty("fonttest", false);
            target.Settings["fonttest"] = new Font("Times New Roman", 7, FontStyle.Italic);
            target.ResetToDefaultValue("fonttest");
            Assert.AreEqual("Tahoma", ((Font)(target.Settings["fonttest"])).FontFamily.Name);
            target.ResetToDefaultValue("doubletest2");
            Assert.AreEqual(3.14, target.Settings["doubletest2"]);
            target.ResetToDefaultValue("inttest");
            Assert.AreEqual(12, target.Settings["inttest"]);
        }

        [TestMethod()]
        public void TestScenario2()
        {
            Settings4Net target = new Settings4Net();
            target.TrackModificationTimes = true;
            target.Add("a1", 1, "desc", "cat", "tag", false);
            target.Add("a2", "x", "desc", "cat", "tag");
            target.Add("a3", new DateTime(1999, 12, 7, 4, 3, 2), "desc");
            target.SetMinItemValue("a3", new DateTime(1900, 10, 10));
            target.SetMaxItemValue("a3", new DateTime(2010, 12, 12, 1, 2, 3));

            target.Save("test_scenario2.xml");
            target.Open("test_scenario2.xml");

            target.Settings["a3"] = new DateTime(1899, 2, 3);
            Assert.AreEqual(new DateTime(1999, 12, 7, 4, 3, 2), target.Settings["a3"]);
            target.Settings["a3"] = new DateTime(2100, 1, 1);
            Assert.AreEqual(new DateTime(1999, 12, 7, 4, 3, 2), target.Settings["a3"]);
            Assert.IsNotNull(target.GetModificationDateTime("a3"));
            Assert.IsNotNull(target.GetModificationDateTime("a2"));
            Assert.IsNotNull(target.GetModificationDateTime("a1"));

            try
            {
                target.SetReadonlyProperty("x", false);
                Assert.Fail();
            }
            catch (Exception)
            { }

            target.Settings["x"] = "string";
            target.SetMaxItemValue("x", "won't be set");
            Assert.AreEqual(null, target.GetMaxItemValue("x"));
            Assert.AreEqual(null, target.GetMinItemValue("x"));

            target.SetItemVisibility("x", false);
            Assert.IsNotNull(target.GeneratePropertyGridObject());
            Assert.AreEqual(true, target.GetItemVisibility("a1"));
        }
    }
}
